import {request, handleFetchResultNotification, REQUEST_TYPE} from '../utils';
import stringify from 'Shared/utils/stringify';
import {formatMessage} from './intl';
const baseUrl = 'afterSales/api/v1';
import * as dto from './DTO';

const getListDesc = formatMessage({
    id: 'api.message.getListDesc',
    defaultMessage: '查询总代信息'
});
const getDetailDesc = formatMessage({
    id: 'api.message.getDetailDesc',
    defaultMessage: '查询总代详细信息'
});
const updateDesc = formatMessage({
    id: 'api.message.updateDesc',
    defaultMessage: '编辑总代详细信息'
});
const getPersonnelDesc = formatMessage({
    id: 'api.message.getPersonnelDesc',
    defaultMessage: '查询人员信息'
});
const exportDataDesc = formatMessage({
    id: 'api.message.exportDataDesc',
    defaultMessage: '导出数据'
});
const importTempDesc = formatMessage({
    id: 'api.message.importTempDesc',
    defaultMessage: '导入模板'
});
// 初始化查询
export const initQuery = () => request(`${baseUrl}/ui/dealerServiceInformation/init`, null, dto.init)
    .then(handleFetchResultNotification(REQUEST_TYPE.initQuery));

// 查询列表
export const getList = bodyCondition => {
    const queryString = stringify(bodyCondition);
    return request(`${baseUrl}/dealerServiceInformations/forCurrentUser?${queryString}`, null, dto.list)
        .then(handleFetchResultNotification(REQUEST_TYPE.query, getListDesc));
};

// 查询总代详细信息
export const getDetail = id => request(`${baseUrl}/dealerServiceInformations/${id}`, null, dto.detail)
    .then(handleFetchResultNotification(REQUEST_TYPE.query, getDetailDesc));

// 编辑总代详细信息
export const updateData = (id, data) => request(`${baseUrl}/dealerServiceInformations/${id}`, {
    method: 'PUT',
    headers: {'Content-Type': 'application/json'},
    body: JSON.stringify(data)
}).then(handleFetchResultNotification(REQUEST_TYPE.submit, updateDesc));

// 查询人员信息
export const getPersonnels = pageOption => {
    const queryString = stringify(pageOption);
    return request(`${baseUrl}/personnels/forDealerServiceInformation?${queryString}`, null, dto.personnel)
        .then(handleFetchResultNotification(REQUEST_TYPE.query, getPersonnelDesc));
};

//导出信息
export const exportData = queryCondition => {
    const queryString = stringify(queryCondition);
    return request(`${baseUrl}/dealerServiceInformations/export/forCurrentUser?${queryString}`)
        .then(handleFetchResultNotification(REQUEST_TYPE.submit, exportDataDesc));
};

//导入清单模板
export const importTemp = () => request(`${baseUrl}/specialPartPriceCoefficients/forDealerServiceInformation/importTemp`)
    .then(handleFetchResultNotification(REQUEST_TYPE.submit, importTempDesc)).then(res => res.data);

//导入批量修改分类标准模板
export const importDealerTemp = () => request(`${baseUrl}/dealerServiceInformations/importTemp`)
    .then(handleFetchResultNotification(REQUEST_TYPE.submit, importTempDesc)).then(res => res.data);

// 查询备件列表
export const partQuery = queryCondition => {
    const queryString = stringify(queryCondition);
    return request(`${baseUrl}/parts/forDealerServiceInformation?${queryString}`, null, null)
        .then(handleFetchResultNotification(REQUEST_TYPE.query, getListDesc));
};
